using System;
using System.Collections;
using System.DirectoryServices;

namespace BEGDC.CC.Entity
{
	#region CostCenterRelatedCost

	/// <summary>
	/// CostCenterRelatedCost object for NHibernate mapped table 'CostCenterRelatedCost'.
	/// </summary>
	[Serializable]
	public class CostCenterRelatedCost : System.IComparable
		{
		#region Member Variables
		
		protected int _id;
		protected string _costCenterCode;
		protected decimal _cost;
		protected decimal _actualCost;
		protected decimal _actualTransferCost;
		protected int _costType;
		protected PaymentInfo _paymentInfo;
		protected static String _sortExpression = "Id";
		protected static SortDirection _sortDirection = SortDirection.Ascending;

		#endregion

		#region Constructors

		public CostCenterRelatedCost() { }

		public CostCenterRelatedCost( string costCenterCode, decimal cost, decimal actualCost, int costType, PaymentInfo paymentInfo )
		{
			this._costCenterCode = costCenterCode;
			this._cost = cost;
			this._actualCost = actualCost;
			this._costType = costType;
			this._paymentInfo = paymentInfo;
		}

		#endregion

		#region Public Properties

		public int Id
		{
			get {return _id;}
			set {_id = value;}
		}

		public string CostCenterCode
		{
			get { return _costCenterCode; }
			set
			{
				if ( value != null && value.Length > 20)
					throw new ArgumentOutOfRangeException("Invalid value for CostCenterCode", value, value.ToString());
				_costCenterCode = value;
			}
		}

		//TODO: encrype, add by kevin
		public decimal Cost
		{
			get { return _cost; }
			set { _cost = value; }
		}

		public decimal DecryptedCost
		{
			get { return EncryptHelper.Decrypt(_cost); }
			set { _cost = EncryptHelper.Encrypt(value); }
		}

		public decimal ActualCost
		{
			get { return _actualCost; }
			set { _actualCost = value; }
		}

		public decimal DecryptedActualCost
		{
			get { return EncryptHelper.Decrypt(_actualCost); }
			set { _actualCost = EncryptHelper.Encrypt(value); }
		}

		public decimal ActualTransferCost
		{
			get { return _actualTransferCost; }
			set { _actualTransferCost = value; }
		}

		public decimal DecryptedActualTransferCost
		{
			get { return EncryptHelper.Decrypt(_actualTransferCost); }
			set { _actualTransferCost = EncryptHelper.Encrypt(value); }
		}

		public int CostType
		{
			get { return _costType; }
			set { _costType = value; }
		}

		public PaymentInfo PaymentInfo
		{
			get { return _paymentInfo; }
			set { _paymentInfo = value; }
		}

        public static String SortExpression
        {
            get { return _sortExpression; }
            set { _sortExpression = value; }
        }

        public static SortDirection SortDirection
        {
            get { return _sortDirection; }
            set { _sortDirection = value; }
        }
		#endregion
		
        #region IComparable Methods
        public int CompareTo(object obj)
        {
			if (!(obj is CostCenterRelatedCost))
				throw new InvalidCastException("This object is not of type CostCenterRelatedCost");
			
			int relativeValue;
			switch (SortExpression)
			{
				case "Id":
					relativeValue = this.Id.CompareTo(((CostCenterRelatedCost)obj).Id);
					break;
				case "CostCenterCode":
						relativeValue = ( this.CostCenterCode != null ) ? this.CostCenterCode.CompareTo(((CostCenterRelatedCost)obj).CostCenterCode) : -1;
					break;
				case "Cost":
					relativeValue = this.DecryptedCost.CompareTo(((CostCenterRelatedCost)obj).DecryptedCost);
//					relativeValue = this.Cost.CompareTo(((CostCenterRelatedCost)obj).Cost);
					break;
				case "ActualCost":
						relativeValue = this.DecryptedCost.CompareTo(((CostCenterRelatedCost)obj).DecryptedActualCost);
//						relativeValue = ( this.ActualCost != Convert.ToDecimal( null) ) ? this.ActualCost.CompareTo(((CostCenterRelatedCost)obj).ActualCost) : -1;
					break;
				case "ActualTransferCost":
					relativeValue = this.DecryptedCost.CompareTo(((CostCenterRelatedCost)obj).DecryptedActualTransferCost);
					break;
				case "CostType":
						relativeValue = ( this.CostType != Convert.ToInt32( null) ) ? this.CostType.CompareTo(((CostCenterRelatedCost)obj).CostType) : -1;
					break;
                default:
                    goto case "Id";
			}
            if (CostCenterRelatedCost.SortDirection == SortDirection.Ascending)
				relativeValue *= -1;
			return relativeValue;
		}
		#endregion
	}

	#endregion
}
